package 品牌管理;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class 查询一个品牌 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入查询品牌id:");
         int id4Query=sc.nextInt();

         ArrayList<Brand> brandList = new  ArrayList<>();
         Brand b =null;

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
            String user = "root";
            String pass="1234";
            conn = DriverManager.getConnection(url, user, pass);

            String sql = "SELECT * FROM t_brand WHERE id = ?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id4Query);
            rs = pstmt.executeQuery();

            while (rs.next()){
                int id = rs.getInt("id");

                String brandName = rs.getString("brand_name");
                String companyName = rs.getString("company_name");
                int ordered = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
//                System.out.println(id+brandName+companyName+ordered+description+status);
                  b=new Brand(id,brandName,companyName,ordered,description,status);
//                 brandList.add(b);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
//        for (Brand brand : brandList) {
//            System.out.println(brand);
        System.out.println("所查品牌是：");
        System.out.println(b);
        }

    }

